<template>
  <div class="page-search">
    <el-card class="box-card">
      <el-row :gutter="10">
        <el-form :model="searchForm" :label-width="searchConfigs.labelWidth" ref="searchRef">
          <template v-for="item in searchConfigs.formItems" :key="item.prop">
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <el-form-item :label="item.label">
                <template v-if="item.type === 'input'">
                  <el-input v-model="searchForm[item.prop]" :placeholder="item.placeholder" />
                </template>
                <template v-else-if="item.type === 'select'">
                  <el-select v-model="searchForm[item.prop]" :placeholder="item.placeholder">
                    <template v-for="option in item.options" :key="option.label">
                      <el-option v-bind="option" />
                    </template>
                  </el-select>
                </template>
                <template v-else-if="item.type === 'time'">
                  <el-time-picker
                    v-model="searchForm[item.prop]"
                    is-range
                    arrow-control
                    range-separator="-"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                  />
                </template>
                <template v-else-if="item.type === 'number'">
                  <el-input-number v-model="searchForm[item.prop]" :min="1" />
                </template>
                <template v-else-if="item.type === 'checkbox'">
                  <el-checkbox v-model="searchForm[item.prop]" :label="item.label" size="large" />
                </template>
              </el-form-item>
            </el-col>
          </template>
        </el-form>
      </el-row>

      <div class="footer">
        <el-button type="primary" @click="handelSubmit" :icon="Search">搜索</el-button>
        <el-button :icon="RefreshRight" @click="handelReset">重置</el-button>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { RefreshRight, Search } from '@element-plus/icons-vue';
import type { FormInstance } from 'element-plus';
// import useBaseStore from '@/store/base';
// import { storeToRefs } from 'pinia';

const emit = defineEmits(['onQuery', 'onReset']);

interface IProps {
  pageName: string;
  searchConfigs: {
    labelWidth?: string;
    formItems: any[];
  };
}

const props = defineProps<IProps>();

const defaultSearchForm: any = {};

props.searchConfigs.formItems.forEach((item) => {
  defaultSearchForm[item.prop];
});
const searchForm = ref(defaultSearchForm);

const searchRef = ref<FormInstance>();

// const baseStore = useBaseStore();
// const { sysuserList } = storeToRefs(baseStore);

const handelSubmit = () => {
  emit('onQuery', props.pageName, searchForm.value);
};

const handelReset = () => {
  searchRef.value?.resetFields();
  searchForm.value = {} as any;
  emit('onReset', props.pageName);
};
</script>

<style lang="less" scoped>
.page-search {
  .el-row {
    width: 100%;

    :deep(.el-form) {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
    }
  }

  .footer {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
